using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace PongTeam
{

    public class PongGame : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        private KeyboardState _keyboardState;
        private MouseState _mouseState;

        private Raquette _player1;
        private Raquette _player2;
        private Balle _balle;
        private Sprite _fond;

        private SpriteFont _policeScore;

        private bool _isPaused;
        private Song _song;

        private float _rapportWidth;
        private float _rapportHeight;

        private bool _isHuman = true;
        private bool _isCPU = false;

        SoundEffect _rebondbar;

        public PongGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            _rapportWidth = Window.ClientBounds.Width / 1920.0f;
            _rapportHeight = Window.ClientBounds.Height / (Window.ClientBounds.Height / (float)Window.ClientBounds.Width * 1920.0f);

            _balle = new Balle(Window.ClientBounds.Width, Window.ClientBounds.Height);
            _balle.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);

            _player1 = new Raquette(Window.ClientBounds.Width, Window.ClientBounds.Height, 1,_isHuman);
            _player1.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);

            _player2 = new Raquette(Window.ClientBounds.Width, Window.ClientBounds.Height, 2,_isCPU);
            _player2.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);

            _fond = new Sprite();
            _fond.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);

            


            _isPaused = true;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            _balle.LoadContent(Content, "Textures/balletron");
            _player1.LoadContent(Content, "Textures/raquettetron");
            _player2.LoadContent(Content, "Textures/raquettetron");
            _fond.LoadContent(Content, "Textures/backgroundtron");
            _rebondbar = Content.Load<SoundEffect>("Sounds/rebondbar");
            _song = Content.Load<Song>("Sounds/tron");
            MediaPlayer.Play(_song);
            MediaPlayer.IsRepeating = true;

            _policeScore = Content.Load<SpriteFont>("Polices/ScorePolice");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            _keyboardState = Keyboard.GetState();
            _mouseState = Mouse.GetState();

            if (!_isPaused)
            {
                _balle.Update(gameTime, _player1.RectangleRaquette, _player2.RectangleRaquette, _rebondbar);
                _player1.HandleInput(_keyboardState, _mouseState, _balle.Position.Y);
                _player2.HandleInput(_keyboardState, _mouseState, _balle.Position.Y);

                _player1.Update(gameTime);
                _player2.Update(gameTime);

                CheckIfBallOut();
            }
            else
            {
                if (_keyboardState.IsKeyDown(Keys.Space))
                {
                    _isPaused = false;
                }
            }
            if (_keyboardState.IsKeyDown(Keys.Escape))
                this.Exit();
            base.Update(gameTime);
        }

        private void CheckIfBallOut()
        {
            if (_balle.Position.X <= 0)
            {
                _player2.score++;
                _balle.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);
                _balle.Position = new Vector2(Window.ClientBounds.Width / 2 - _balle.Texture.Width * _rapportWidth / 2,
                                             Window.ClientBounds.Height / 2 - _balle.Texture.Height * _rapportHeight / 2);
                _isPaused = true;
            }
            else if (_balle.Position.X >= Window.ClientBounds.Width - _balle.Texture.Width * _rapportWidth)
            {
                _player1.score++;
                _balle.Initialize(Window.ClientBounds.Width, Window.ClientBounds.Height);
                _balle.Position = new Vector2(Window.ClientBounds.Width / 2 - _balle.Texture.Width * _rapportWidth / 2,
                                             Window.ClientBounds.Height / 2 - _balle.Texture.Height * _rapportHeight / 2);
                _isPaused = true;
            }
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.DarkGray);
            spriteBatch.Begin();

            _fond.Draw(spriteBatch, gameTime, 1);

            Vector2 scoreP1Size = _policeScore.MeasureString(_player1.score.ToString());
            Vector2 scoreP1Position = new Vector2((float)(Window.ClientBounds.Width / 2.325 - scoreP1Size.X / 2),
                                                  (float)(Window.ClientBounds.Height * 0.07 - scoreP1Size.Y / 2));
            spriteBatch.DrawString(_policeScore, _player1.score.ToString(), scoreP1Position, Color.White);

            Vector2 scoreP2Size = _policeScore.MeasureString(_player2.score.ToString());
            Vector2 scoreP2Position = new Vector2((float)(Window.ClientBounds.Width / 1.75 - scoreP2Size.X / 2),
                                                  (float)(Window.ClientBounds.Height * 0.07 - scoreP2Size.Y / 2));
            spriteBatch.DrawString(_policeScore, _player2.score.ToString(), scoreP2Position, Color.White);

            _player1.Draw(spriteBatch, gameTime, 2);
            _player2.Draw(spriteBatch, gameTime, 2);
            _balle.Draw(spriteBatch, gameTime, 2);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
